﻿using AppDataStruct;
using KWindow.KPageCore;
using LogLib;
using PrintExcel;
using System;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace YSLPages
{
    public partial class PagePaperPrinting : PageControlBase
    {
      
        private bool _printResult = false;

        private PictureBox pictureBox1 = null;

        private Image imgPrinting = null;

        private Image imgPrintFailed = null;

        private Label labelInputTip = null;

        private PreApplyInfo _appInfo = null;

        private PaperPrinting _print = null;

        private SFZInfo _sfz = new SFZInfo();

        public PagePaperPrinting()
        {
            InitializeComponent();
            SetProcessBase(ProcessPaperPrinting.Instance);
        }

        private void GetControls()
        {
            labelInputTip = GetControl<Label>("labelInputTip") as Label;
            pictureBox1 = GetControl<PictureBox>("pictureBox1") as PictureBox;
            imgPrintFailed = GetImage("PrintFailed");
            imgPrinting = GetImage("Printing");
        }

        //初始化窗体数据
        public override void InitCtrls()
        {
            base.InitCtrls();
            GetControls();
            _print = new PaperPrinting();          
            if (!_print.Init())
            {
                MessageBox.Show(_print.ErrorMessage);
            }
        }

        //初始化窗体数据
        public override void InitData()
        {
            base.InitData();
            pictureBox1.Image = imgPrinting;
            _printResult = false;
            _sfz = Process.GetValue<SFZInfo>("SFZInfo");
            _appInfo = Process.GetValue<PreApplyInfo>("PreApplyInfo");
            
            Speak("正在打印申请表，打印完成后请从出纸口取走申请表");
            SetTips("正在打印申请表，\n打印完成后请从出纸口取走申请表");
        }

        /// <summary>
        /// 保存窗体数据(窗体本次做完后, 进入下一步之前)
        /// </summary>
        public override void UpdateData()
        {

        }

        /// <summary>
        /// 清理窗体数据(窗体本次做完后)
        /// </summary>
        public override void ClearData()
        {
            base.ClearData();
            _printResult = false;
            _appInfo = null;
        }

        /// <summary>
        /// 窗体关闭时
        /// </summary>
        public override void Close()
        {
            base.Close();
        }

        private void BeginPrinting()
        {
            if(!_print.Inited)
            {
                if (!_print.Init())
                {
                    MessageBox.Show(_print.ErrorMessage);
                    return;
                }
            }
            
            Task.Factory.StartNew(() =>
            {
                if(_print.OnPrepared(_sfz, _appInfo))
                {
                    Log.WriteAsync("准备打印成功");                  
                    string msg = "";
                    if(_print.DoPrint(out msg))
                    {
                        Thread.Sleep(10000);
                        OnPrintingCompleted();
                    }
                    else
                    {
                        MessageBox.Show("打印失败:" + msg);
                        Process.ToHome();
                    }
                }
                else
                {
                    OnPrintFailed(_print.ErrorMessage);
                }
            });
        }

        public override void StartWork()
        {
            base.StartWork();
            if (IsMainPage)
            {
                Speak("正在打印申请表格，请稍候");
                BeginPrinting();
            }           
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        public override void OnCountdownCompleted(object sender, EventArgs e)
        {
            base.OnCountdownCompleted(sender, e);
        }

        protected override void btnBack_Click(object sender, EventArgs e)
        {
            StopCountdown();
            base.btnBack_Click(sender, e);
        }

        protected override void btnNext_Click(object sender, EventArgs e)
        {
            StopCountdown();
            base.btnNext_Click(sender, e);
        }

        protected override void btnCancel_Click(object sender, EventArgs e)
        {
            StopCountdown();
            base.btnCancel_Click(sender, e);
        }
        private void OnPrintingCompleted()
        {
            _printResult = true;
            Log.WriteAsync("申请表格打印成功!");
            StopCountdown();
            Action handler = () =>
            {
                Speak("申请表格打印完成，请取走您的申请表");
                UpdateData();
                ClearData();
                Process.ToHome();
            };
            this.Invoke(handler);
        }

        private void OnPrintFailed(string outParams)
        {
            string msg = string.Format("打印申请表失败: \n{0}", outParams);
            Log.WriteAsync(msg);
            Action<string> handler = (e) =>
            {
                Speak("打印申请表格失败，请联系工作人员");
                pictureBox1.Image = imgPrintFailed;
                SetTips(msg);
                CountdownSecond = 30;
                StartCountdown();
            };
            this.Invoke(handler, msg);
        }

        private void SetTips(string content)
        {
            labelInputTip.Text = content;
            int w = labelInputTip.Width;
            int x = (this.Width - w) / 2;
            labelInputTip.Left = x;
        }
    }
}
